(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:FxgradeCtrl
     * @description
     * # FxgradeCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('FxgradeCtrl', FxgradeCtrl);

    FxgradeCtrl.$inject = ['$scope', 'dataService', '$state', 'helper', '$location', 'sessionService', '$rootScope', 'ERP_CONFIG'];

    function FxgradeCtrl($scope, dataService, $state, helper, $location, sessionService, $rootScope, ERP_CONFIG) {

        $scope.listAction = function() {
            $rootScope.whiteListUser();

            $scope.pageSize = 10;
            $scope.getList();
        }

        //查询分销商列表
        $scope.getList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize,
                    store_id: $rootScope.store_id
                }
            };

            if ($scope.grade_id) {
                query.content = {
                    store_id: $rootScope.store_id,
                    grade_id: $scope.grade_id
                };
            }

            dataService.fxgradeGet(query, true).then(function(r) {
                if (r) {
                    if ($scope.grade_id) {
                        $scope.fxgrade = r.grade_list[0];
                        console.error("fxgrade-->", $scope.fxgrade);
                    } else {
                        $scope.grade_list = r.grade_list;
                        $scope.total_count = r.total_count;
                    }
                }
            });
        }

        $scope.getProductList = function(pageNo) {
            var query = {
                "content": {
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize
                }
            };

            if ($scope.product_id) {
                query.content = { product_id: $scope.product_id };
            }

            dataService.fxproductGet(query).then(function(list) {
                if (list) {
                    if ($scope.product_id) {
                        $scope.product_data = list.product_list[0];
                    } else {
                        $scope.product_list = list.product_list;
                        $scope.total_count = list.total_count;
                    }
                }
            });
        }

        //分销商等级添加
        $scope.addAction = function() {
            $rootScope.whiteListUser();

            //代理等级初始数据
            var grade_id = $location.search().grade_id;
            if (grade_id) {
                $scope.grade_id = grade_id;
                $scope.getList(1);
            } else {
                $scope.fxgrade = {};
                $scope.fxgrade.name = "1级代理商";
                $scope.fxgrade.value = 1;
                $scope.fxgrade.trade_amount = 0;
                $scope.fxgrade.trade_number = 0;
                $scope.fxgrade.the_superior = 0;
                $scope.fxgrade.on_the_superior = 0;
            }

            //产品线设置初始数据
            var product_id = $location.search().product_id;
            if (product_id) {
                $scope.product_id = product_id;
                $scope.getProductList(1);
            } else {
                var data = {
                    // "grade_id": 0,
                    "name": "产品1线",
                    "proportion": "10",
                    "remark": "",
                };
            }
            $scope.product_data = data;
        }

        //点击下一步 产品线设置，保存代理商等级
        $scope.fxgradeAdd = function() {
            if (!$scope.fxgrade.value) {
                helper.myAlert("error ：等级值不能为空，返回上步修改！");
                return false;
            }

            if (!$scope.fxgrade.trade_amount) {
                helper.myAlert("error ：交易金额不能为空，返回上步修改！");
                return false;
            }

            if (!$scope.fxgrade.trade_number) {
                helper.myAlert("error ：交易笔数不能为空，返回上步修改！");
                return false;
            }

            var data = {
                content: $scope.fxgrade
            };

            if ($scope.fxgrade.grade_id) {
                dataService.fxgradeUpdate(data).then(function(r) {
                    if (r.errmsg == "ok") {

                    } else {
                        helper.myAlert("error:" + r.errmsg);
                        return false;
                    }
                });
            } else {
                dataService.fxgradeAdd(data).then(function(r) {
                    if (r.errmsg == "ok") {
                        $scope.fxgrade.grade_id = r.grade_id;
                        $scope.grade_id = r.grade_id;
                        $scope.product_data.grade_id = $scope.grade_id;
                    } else {
                        helper.myAlert("error:" + r.errmsg);
                        return false;
                    }
                });
            }
        }

        //保存产品线，并触发已加入产品线商品列表
        $scope.nextAdd = function() {
            var pp = dataService.promise();

            pp = pp.then(function() {
                return $scope.productAdd();
            }).then(function() {
                $scope.setItem();
            });
        }

        //产品线添加
        $scope.productAdd = function(param) {
            if ($scope.product_data.name.length > 5) {
                helper.myAlert("error:产品线名称不能超过5个字");
                return false;
            }

            if ($scope.product_data.proportion > 100) {
                helper.myAlert("error:返点折扣不能大于100");
                return false;
            }

            var product_id = $location.search().product_id;
            if (product_id) {
                var data = {
                    "content": $scope.product_data,
                };
                dataService.fxproductUpdate(data).then(function(result) {
                    $scope.product_id = result.product_id;
                });
            } else {
                if (!$scope.grade_id && !$scope.product_data.grade_id) {
                    helper.myAlert("没有创建代理商等级，无法创建产品线");
                    return false;
                }
                $scope.product_data.grade_id = $scope.grade_id;

                var data = {
                    "content": $scope.product_data
                };
                // console.error("data--->", data);
                // return false;

                return dataService.fxproductAdd(data).then(function(result) {
                    if (result.product_id) {
                        $scope.product_id = result.product_id;
                        $scope.product_data.product_id = result.product_id;
                    }
                    return true;
                });
            }
        }

        //点击下一步设置商品
        $scope.setItem = function() {
            $scope.checkedBroadcast();
        }

        //已加入产品线商品
        $scope.checkedBroadcast = function() {
            $rootScope.$broadcast("event:productcat-checked-item-list", { product_id: $scope.product_id });
        }

        //商品列表
        $scope.broadcast = function() {
            $rootScope.$broadcast("event:productcat-item-list", { product_id: $scope.product_id });
        }

        //活动列表页
        $scope.huodongBroadcast = function() {
            $rootScope.$broadcast("event:productcat-huodong-list", { product_id: $scope.product_id });
        }

        $scope.itemGet1 = function(fenxiao_item_num) {
            $scope.fenxiao_item_num = fenxiao_item_num;
        }

        //返回代理等级列表页
        $scope.goList = function() {
            $state.go("fxgrade.list");
        }

        //tab切换
        $scope.tabCut = function($event) {
            $event.preventDefault();
            $($event.currentTarget).tab('show');
        }
    }
})();